package com.jah.picavue.client;

import java.util.HashMap;

import com.google.gwt.user.client.History;
import com.jah.picavue.client.events.AlbumFeedRequestedEvent;
import com.jah.picavue.client.events.AlbumSelectionEvent;
import com.jah.picavue.client.events.CancelTimerEvent;
import com.jah.picavue.client.events.GotUserDataEvent;
import com.jah.picavue.client.events.GotAlbumDataEvent;
import com.jah.picavue.client.model.Album;
import com.jah.picavue.client.view.AlbumView;
import com.jah.picavue.client.view.IndexView;

@SuppressWarnings("unchecked")
public class ViewManager implements Config,
	GotUserDataEvent.Handler, GotAlbumDataEvent.Handler,
	AlbumSelectionEvent.Handler {

	private HashMap<String, ?> requestedFeeds = new HashMap();
	private HashMap<String, Album> readyAlbums = new HashMap();
	private IndexView index = IndexView.INSTANCE;
	private AlbumView album_view;

	private String current_view = "index";
	private String active_album;

	// private int clientWidth = 0;
	// private int clientHeight = 0;

	public final static ViewManager INSTANCE = new ViewManager();
	private ViewManager(){
		EventBus.get().addHandler(GotUserDataEvent.TYPE, this);
		EventBus.get().addHandler(GotAlbumDataEvent.TYPE, this);
		EventBus.get().addHandler(AlbumSelectionEvent.TYPE, this);
	};

	
	public void onGotUserData(GotUserDataEvent event) {
		index.init();
	}

	
	public void onGotAlbumData(GotAlbumDataEvent event) {
		Album data = event.getData();
		String id = data.getID();
		readyAlbums.put(id, data);
		requestedFeeds.remove(id);
		doAlbumView(null, null);
	}

	
	public void onAlbumSelected(AlbumSelectionEvent event) {
		String selected_id = event.getSelectedAlbumID();
		if (selected_id == null) return; // Bad ID
		current_view = event.getView();
		active_album = selected_id;
		if (!History.getToken().equals(current_view + "+" + active_album)) {
			History.newItem(current_view + "+" + active_album, false);
		}
		if (!readyAlbums.containsKey(selected_id)) {
			if (!requestedFeeds.containsKey(selected_id)) {
				AlbumFeedRequestedEvent evt = new AlbumFeedRequestedEvent();
				evt.setID(selected_id);
				EventBus.get().fireEvent(evt);
			} else {
				// I guess we just wait for a GotAlbumDataEvent
			}
		} else {
			doAlbumView(null, null);
		}
	}

	
	public void doIndex(String view) {
		index.doView();
	}

	
	public void doAlbumView(String view, String albumID) {
		if (view != null && !view.equals("slideshow")) {
			EventBus.get().fireEvent( new CancelTimerEvent() );
		}
		if (albumID != null && !albumID.equals(active_album)) {
			// changed album
			AlbumSelectionEvent evt = new AlbumSelectionEvent();
			evt.setSelectedAlbumID(albumID);
			evt.setView(view);
			EventBus.get().fireEvent(evt);
		} else if (albumID != null && albumID.equals(active_album)) {
			// same album different view
			if (current_view.equals("gallery") && view.equals("slideshow")) {
				this.album_view.setSlideShowStart();
			} else {
			  this.album_view.setCursor(0);
			}
			this.album_view.redraw(view);
		} else if (view == null && albumID == null) {
			if (album_view == null || (active_album != null && !active_album.equals(album_view.getAlbumID()))) {
				// first time we selected this album - came from onGotAlbumData
				album_view = new AlbumView(readyAlbums.get(active_album), current_view);
			} else {
				this.album_view.setCursor(0);
				this.album_view.redraw(current_view);
			}
		}
	}
}
